package console

import (
	"gin-demo/common"
	"gin-demo/container"
	"gin-demo/entity"
	"github.com/jinzhu/gorm"
)

type ClusterDaoImp struct {
}

func (c ClusterDaoImp) List(pageRequest common.PageRequest) (*entity.ClusterList, error) {

	offset, limit := common.PageHandle(pageRequest)
	var clusters []*entity.Cluster
	var clusterDB = container.Get("db").(*gorm.DB)
	err := clusterDB.
		Table("cluster").
		Offset(offset).
		Limit(limit).
		Find(&clusters).Error
	var count int64
	err = clusterDB.
		Table("cluster").
		Count(&count).Error
	if err != nil {
		return nil, err
	}

	var pageResponse common.PageResponse
	pageResponse.Total = count
	pageResponse.PageInfo = pageRequest
	clusterList := &entity.ClusterList{
		PageResponse: pageResponse,
		Clusters:     clusters,
	}
	return clusterList, nil

}
